package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseAuditApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseAuditVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@RestController
public class CourseAuditController implements CourseAuditApi {

    @Autowired
    private CourseBaseService courseBaseService;

    @PostMapping("m/course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseModel model) {
        PageVO<CourseBaseDTO> pageVO = courseBaseService.queryCourseList(params, model, null);
        return pageVO;
    }

    @PostMapping("m/courseReview/approve")
    public void approveCourse(@RequestBody CourseAuditVO vo) {

        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.auditvo2dto(vo);
        courseBaseService.approveCourse(dto);

    }

    @GetMapping("course/preview/{courseBaseId}")
    public Object preview(@PathVariable Long courseBaseId) {

        //1.从业务层获得数据模型
        Map<String, Object> dataMap = courseBaseService.preview(courseBaseId, null);

        //2.结果视图器
        ModelAndView modelAndView = new ModelAndView("learing_article", dataMap);
        return modelAndView;
    }
}
